import request from '../utils/request'
// 课程分类
export const catePost = (cateObj)=>{
  return request.post('/classes/LearnCate',cateObj)
}

export const cateGet = ()=>{
  return request.get('/classes/LearnCate')
}

export const catePut = (objectId,cate)=>{
  return request.put(`/classes/LearnCate/${objectId}`,cate)
}

// 图文课程
export const artPost = (artObj)=>{
  return request.post('/classes/LearnArticle',artObj)
}

export const artGet = (params,sort,filter)=>{
  //表单查询
  delete params.pageSize
  delete params.current
  delete params.created_at
  for(let attr in params){  //不要将查询字段值为空的条件，带给后端
    if(params[attr]==''){
      delete params[attr]
    }
  }
  
  // console.log(sort);
  //价格排序
  let order = ''
  if(sort.price){
    order = sort.price === 'ascend' ? `&order=price` : '&order=-price'
  } 
  //上架状态过滤
  console.log(filter);
  if(filter.online&&filter.online.length==1){
    let bool = filter.online.includes('open');
    params.online = bool
    console.log(bool);
  }

  let search = JSON.stringify(params)

  return request.get(`/classes/LearnArticle?where=${search}${order}`)
  // 等同于
  // return request.get(`/classes/LearnArticle?where={"artname":"${params.artname}"}`)
}